import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Teststatefulset:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/1_get_statefulset_list.yaml'))
    def test_get_statefulset_list(self, statefulsetinfo):
        # 获取statefulset列表接口-正常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default')
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/2_create_statefulset.yaml'))
    def test_create_statefulset(self, statefulsetinfo):
        # 创建statefulset接口-正常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default')
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        statefulsetname = req.json()['metadata']['name']
        globals()['statefulsetname'] = statefulsetname
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/3_get_specific_statefulset.yaml'))
    def test_get_specific_statefulset(self, statefulsetinfo):
        # 获取指定statefulset接口-正常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/4_update_statefulset.yaml'))
    def test_update_statefulset(self, statefulsetinfo):
        # 更新指定statefulset接口-正常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/5_update_section_statefulset.yaml'))
    def test_update_section_statefulset(self, statefulsetinfo):
        # 部分更新指定statefulset的接口-正常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        header = statefulsetinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/6_delete_specific_statefulset.yaml'))
    def test_delete_specific_statefulset(self, statefulsetinfo):
        # 删除statefulset接口-正常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/1_get_statefulset_list_exception.yaml'))
    def test_get_statefulset_list_exception(self, statefulsetinfo):
        # 获取statefulset列表接口-异常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default')
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/2_create_statefulset_exception.yaml'))
    def test_create_statefulset_exception(self, statefulsetinfo):
        # 创建statefulset接口-异常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default')
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/3_get_specific_statefulset_exception.yaml'))
    def test_get_specific_statefulset_exception(self, statefulsetinfo):
        # 获取指定statefulset接口-异常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/4_update_statefulset_exception.yaml'))
    def test_update_statefulset_exception(self, statefulsetinfo):
        # 更新指定statefulset接口-异常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/5_update_section_statefulset_exception.yaml'))
    def test_update_section_statefulset_exception(self, statefulsetinfo):
        # 部分更新指定statefulset的接口-异常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        data = statefulsetinfo['request']['data']
        header = statefulsetinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.statefulset
    @pytest.mark.parametrize('statefulsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/statefulset/6_delete_specific_statefulset_exception.yaml'))
    def test_delete_specific_statefulset_exception(self, statefulsetinfo):
        # 删除statefulset接口-异常
        base_url = datas.baseUrl + Teststatefulset.url_unify
        url = base_url + statefulsetinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{statefulsetname}', globals()['statefulsetname'])
        method = statefulsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == statefulsetinfo['validate'][0]['equals']['status_code']
